//
//  HYNavigationController.swift
//  weibo-dg
//
//  Created by 源 何 on 16/9/23.
//  Copyright © 2016年 何源. All rights reserved.
//

import UIKit

class HYNavigationController: UINavigationController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //隐藏默认的 NavigationBar
        navigationBar.isHidden = true
    }

    //重写 push方法
    //viewController 是被 push 的controller
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        //如果不是栈底控制器,根控制器不需要处理
        if childViewControllers.count > 0 {
            
            //隐藏底部的tabbar
            viewController.hidesBottomBarWhenPushed = true
            
            //判断控制器的类型
            if let vc = viewController as? HYBaseViewController {
                
                var title = "返回"
                
                //判断控制器的级数
                if childViewControllers.count == 1 {
                    
                    //title 显示首页的标题
                    title = childViewControllers.first?.title ?? "返回"
                    
                }
                
                //取出自定义的navTtem
                vc.navItem.leftBarButtonItem = UIBarButtonItem(title: title, target: self, action: #selector(popToParent), isBack: true)
                
            }
        }
        
        super.pushViewController(viewController, animated: true)
    }
    
    @objc private func popToParent() {
        popViewController(animated: true)
    }


}
